package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.dto.SeatDto;
import com.hdax.dm.mappers.SchedulerSeatMapper;
import com.hdax.dm.scheduler.DmScheduler;
import com.hdax.dm.scheduler.DmSchedulerSeat;
import com.hdax.dm.service.SchedulerSeatSevrice;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SchedulerSeatSevriceImpl extends ServiceImpl<SchedulerSeatMapper, DmSchedulerSeat> implements SchedulerSeatSevrice {
    @Override//座位信息获取
    public SeatDto seatList(String scheduleId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("scheduleId",scheduleId);
        List<DmSchedulerSeat> list= list(wrapper);
        Map<Long,List<DmSchedulerSeat>> map = new HashMap<>();
        map = list.stream().collect(Collectors.groupingBy(DmSchedulerSeat::getX));
        List<String> stringList = new ArrayList<>();
        SeatDto dto = new SeatDto();
        map.values().forEach(seats->{
            StringBuffer buffer =new StringBuffer();
            List<DmSchedulerSeat> seatList = seats;
            seatList.forEach(seat ->{
                if (seat.getStatus() ==0L){
                    buffer.append("_");
                }else{
                    if(seat.getAreaLevel() == 1L){
                        buffer.append("a");
                    } else if (seat.getAreaLevel() ==2L) {
                        buffer.append("b");
                    }else if (seat.getAreaLevel() ==3L) {
                        buffer.append("c");
                    } else if (seat.getAreaLevel() == 0L) {
                        buffer.append("_");
                    }
                }
            });
            stringList.add(buffer.toString());

        });
        dto.setSeatArray(stringList);


        return dto;
    }

    @Override//座位状态获取
    public List<DmSchedulerSeat> seatList(Long scheduleId) {

        QueryWrapper wrapper =new QueryWrapper<>();
        wrapper.eq("scheduleId",scheduleId);
        wrapper.eq("status",1L);
        wrapper.eq("orderNo","");
        wrapper.isNull("userId");

        return list(wrapper);
    }

    @Override//根据x，y，SchedulerId获取areaLevel
    public List<DmSchedulerSeat> seatXYScheIds(Long x, Long y, Long scheduleId) {

        QueryWrapper wrapper =new QueryWrapper<>();
        wrapper.eq("scheduleId",scheduleId);
        wrapper.eq("x",x);
        wrapper.eq("y",y);

        return list(wrapper);
    }

    @Override//修改座位状态
    public Boolean updateSeat(DmSchedulerSeat seat) {

        return baseMapper.updateById(seat) ==1 ? true:false;
    }

    @Override
    public Boolean updateSeatXy(String orderNo) {
        return baseMapper.updateSeatXY(orderNo);
    }
}
